package com.mdd.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ShopRecord;
import com.mdd.common.enums.NoticeEnum;
import com.mdd.common.mapper.ShopRecordMapper;
import com.mdd.common.util.RedisUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import com.mdd.common.util.sms.Sms;
import com.mdd.front.service.IShopAppRecordService;
import com.mdd.front.validate.ShopAppRecordCreateValidate;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.vo.record.ShopRecordDetailVo;
import com.mdd.front.vo.record.ShopRecordListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class ShopAppRecordServiceImpl implements IShopAppRecordService {
    @Resource
    ShopRecordMapper shopRecordMapper;


    /**
     * 开店申请列表
     *
     * @author cxt
     * @param pageValidate 分页参数
     * @return PageResult<ShopRecordListedVo>
     */
    @Override
    public PageResult<ShopRecordListedVo> list(PageValidate pageValidate, Integer userId) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ShopRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("user_id",userId);

        IPage<ShopRecord> iPage = shopRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ShopRecordListedVo> list = new LinkedList<>();
        for(ShopRecord item : iPage.getRecords()) {
            ShopRecordListedVo vo = new ShopRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setBusinessLicense(UrlUtils.toAbsoluteUrl(item.getBusinessLicense()));
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 开店申请详情
     *
     * @author cxt
     * @param id 主键参数
     * @return ShopRecord
     */
    @Override
    public ShopRecordDetailVo detail(Integer id) {
        ShopRecord model = shopRecordMapper.selectOne(
                new QueryWrapper<ShopRecord>()
                        .eq("id", id)
                        .last("limit 1"));

        org.springframework.util.Assert.notNull(model, "数据不存在");

        ShopRecordDetailVo vo = new ShopRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }




    /**
     * 开店申请新增
     *
     * @author cxt
     * @param createValidate 参数
     */
    @Override
    public void add(ShopAppRecordCreateValidate createValidate) {
        String code = createValidate.getCode();
        boolean right = false;
        // 校验验证码
        int sceneCode = NoticeEnum.SHOP_RECORD_CODE.getCode();
        Object savedMobile = RedisUtils.get("MOBILE_CODE_" + sceneCode + "_" + code);
        if (savedMobile != null && createValidate.getPhone().equals(savedMobile.toString())) {
            right = true;
        }
        Assert.isFalse(!right, "验证码不一致");

        ShopRecord model = new ShopRecord();
        model.setUserId(createValidate.getUserId());
        model.setShopName(createValidate.getShopName());
        model.setShopCategory(createValidate.getShopCategory());
        model.setContactName(createValidate.getContactName());
        model.setPhone(createValidate.getPhone());
        model.setUserName(createValidate.getUserName());
        model.setPassword(createValidate.getPassword());
        model.setBusinessLicense(createValidate.getBusinessLicense());
        model.setIsAgree(createValidate.getIsAgree());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setCreateTime(System.currentTimeMillis() / 1000);
        shopRecordMapper.insert(model);
    }

    @Override
    public AjaxResult<Object> sendCode(String mobile) {
        String code = (int) ((Math.random() * 9 + 1) * 100000) + "";
        String sendSmsCode = Sms.sendSmsCode(code, mobile);
        if ("0".equals(sendSmsCode)) {
            RedisUtils.set("MOBILE_CODE_"+ NoticeEnum.SHOP_RECORD_CODE.getCode() + "_" + code,mobile,300);
            return AjaxResult.success("发送验证码成功");
        }
        return AjaxResult.failed("发送失败");
    }


}
